﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.IDAL.FoodTesting;
using System.Data.SqlClient;
using System.Data;
using StoragePeriod.DBUtility;
using StoragePeriod.Model.FoodTesting;

namespace StoragePeriod.SqlServerDAL.FoodTesting
{
    /// <summary>
    /// 数据访问类:CuFeZnData
    /// </summary>
    public partial class D_CuFeZnData : I_CuFeZnData
    {
        public D_CuFeZnData()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from CuFeZnData");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_CuFeZnData model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CuFeZnData(");
            strSql.Append("MineralTypeID,RawID,RawName,P1,P0,V1,M,Result,FoodTime,FoodNum)");
            strSql.Append(" values (");
            strSql.Append("@MineralTypeID,@RawID,@RawName,@P1,@P0,@V1,@M,@Result,@FoodTime,@FoodNum)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@MineralTypeID", SqlDbType.Int,4),
					new SqlParameter("@RawID", SqlDbType.Int,4),
					new SqlParameter("@RawName", SqlDbType.NVarChar,50),
					new SqlParameter("@P1", SqlDbType.NVarChar,50),
					new SqlParameter("@P0", SqlDbType.NVarChar,50),
					new SqlParameter("@V1", SqlDbType.NVarChar,50),
					new SqlParameter("@M", SqlDbType.NVarChar,50),
					new SqlParameter("@Result", SqlDbType.NVarChar,50),
					new SqlParameter("@FoodTime", SqlDbType.DateTime),
					new SqlParameter("@FoodNum", SqlDbType.Int,4)};
            parameters[0].Value = model.MineralTypeID;
            parameters[1].Value = model.RawID;
            parameters[2].Value = model.RawName;
            parameters[3].Value = model.P1;
            parameters[4].Value = model.P0;
            parameters[5].Value = model.V1;
            parameters[6].Value = model.M;
            parameters[7].Value = model.Result;
            parameters[8].Value = model.FoodTime;
            parameters[9].Value = model.FoodNum;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_CuFeZnData model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CuFeZnData set ");
            strSql.Append("MineralTypeID=@MineralTypeID,");
            strSql.Append("RawID=@RawID,");
            strSql.Append("RawName=@RawName,");
            strSql.Append("P1=@P1,");
            strSql.Append("P0=@P0,");
            strSql.Append("V1=@V1,");
            strSql.Append("M=@M,");
            strSql.Append("Result=@Result,");
            strSql.Append("FoodTime=@FoodTime,");
            strSql.Append("FoodNum=@FoodNum");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@MineralTypeID", SqlDbType.Int,4),
					new SqlParameter("@RawID", SqlDbType.Int,4),
					new SqlParameter("@RawName", SqlDbType.NVarChar,50),
					new SqlParameter("@P1", SqlDbType.NVarChar,50),
					new SqlParameter("@P0", SqlDbType.NVarChar,50),
					new SqlParameter("@V1", SqlDbType.NVarChar,50),
					new SqlParameter("@M", SqlDbType.NVarChar,50),
					new SqlParameter("@Result", SqlDbType.NVarChar,50),
					new SqlParameter("@FoodTime", SqlDbType.DateTime),
					new SqlParameter("@FoodNum", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.MineralTypeID;
            parameters[1].Value = model.RawID;
            parameters[2].Value = model.RawName;
            parameters[3].Value = model.P1;
            parameters[4].Value = model.P0;
            parameters[5].Value = model.V1;
            parameters[6].Value = model.M;
            parameters[7].Value = model.Result;
            parameters[8].Value = model.FoodTime;
            parameters[9].Value = model.FoodNum;
            parameters[10].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CuFeZnData ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CuFeZnData ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_CuFeZnData GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,MineralTypeID,RawID,RawName,P1,P0,V1,M,Result,FoodTime,FoodNum from CuFeZnData ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            E_CuFeZnData model = new E_CuFeZnData();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MineralTypeID"].ToString() != "")
                {
                    model.MineralTypeID = int.Parse(ds.Tables[0].Rows[0]["MineralTypeID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RawID"].ToString() != "")
                {
                    model.RawID = int.Parse(ds.Tables[0].Rows[0]["RawID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RawName"] != null)
                {
                    model.RawName = ds.Tables[0].Rows[0]["RawName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["P1"] != null)
                {
                    model.P1 = ds.Tables[0].Rows[0]["P1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["P0"] != null)
                {
                    model.P0 = ds.Tables[0].Rows[0]["P0"].ToString();
                }
                if (ds.Tables[0].Rows[0]["V1"] != null)
                {
                    model.V1 = ds.Tables[0].Rows[0]["V1"].ToString();
                }
                if (ds.Tables[0].Rows[0]["M"] != null)
                {
                    model.M = ds.Tables[0].Rows[0]["M"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Result"] != null)
                {
                    model.Result = ds.Tables[0].Rows[0]["Result"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FoodTime"].ToString() != "")
                {
                    model.FoodTime = DateTime.Parse(ds.Tables[0].Rows[0]["FoodTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FoodNum"].ToString() != "")
                {
                    model.FoodNum = int.Parse(ds.Tables[0].Rows[0]["FoodNum"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,MineralTypeID,RawID,RawName,P1,P0,V1,M,Result,FoodTime,FoodNum ");
            strSql.Append(" FROM CuFeZnData ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,MineralTypeID,RawID,RawName,P1,P0,V1,M,Result,FoodTime,FoodNum ");
            strSql.Append(" FROM CuFeZnData ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "CuFeZnData";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        /// <summary>
        /// 根据原料ID和检测时间 删除检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByTime(E_CuFeZnData eCuFeZnData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CuFeZnData where RawID=" + eCuFeZnData.RawID + " and FoodTime=CAST('" + eCuFeZnData.FoodTime.ToString() + "' as datetime) and MineralTypeID=" + eCuFeZnData.MineralTypeID);
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除某一次检测记录
        /// </summary>
        /// <param name="eVcData">要删除的检测记录</param>
        /// <returns></returns>
        public int RemoveByFoodNum(E_CuFeZnData eCuFeZnData)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CuFeZnData where RawID=" + eCuFeZnData.RawID + " and FoodTime=CAST('" + eCuFeZnData.FoodTime.ToString() + "' as datetime) and FoodNum=" + eCuFeZnData.FoodNum + " and MineralTypeID=" + eCuFeZnData.MineralTypeID);
            return DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        #endregion  Method
    }
}
